// packageViewpoint/pages/list/list.js
import {getCate, getList} from "../../../service/Viewpoint";
import API from "../../../service/API"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cateList: [], //分类
        viewpoint: {
            list: [],
            loading: false,
            reset: false,
            error: false,
            total: 0
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // this.getViewpointCate();
        this.setData({"viewpoint.loading": true});
    },

    /*
    * 获取动态分类
    * */
    getViewpointCate: async function () {
        try {
            let res = await getCate();
            this._cid = res[0].cid;
            this.setData({
                cateList: res,
                "viewpoint.loading": true
            });
        } catch (e) {
            console.error(e)
        }
    },

    /*
    * 监听列表加载
    * */
    onLoadList: async function (e) {
        try {
            let list = this.data.viewpoint.list;
            let isReset = this.data.viewpoint.reset;
            let paging = e.detail.paging;
            let res = await getList({
                "cid": "5",
                "page": paging.page,
                "per-page": paging.pageSize
            });
            let newList = res.itemList.map(function (item) {
                item.time = item.time.split(" ")[0]
                return item
            })
            list = isReset ? newList : list.concat(newList);
            this.setData({
                "viewpoint.list": list,
                "viewpoint.reset": false,
                "viewpoint.loading": false,
                "viewpoint.total": res.totalCount
            });
        } catch (e) {
            this.setData({"viewpoint.error": true})
        }
    },

    /*
    * tabs切换
    * */
    onTabsChange: function (e) {
        const that = this;
        clearTimeout(that._onTabsChange);
        that._onTabsChange = setTimeout(function () {
            let {index} = e.detail;
            let cateList = that.data.cateList;
            that._cid = cateList[index].cid;
            that.setData({
                "viewpoint.list": [],
                "viewpoint.reset": true,
            })
        }, 300)
    },

    /*
    * 点击查看详情
    * */
    bindItem: function (e) {
        let index = e.currentTarget.dataset.index;
        let list = this.data.viewpoint.list;
        let currentItem = list[index];
        wx.navigateTo({
            url: "/packageViewpoint/pages/detail/detail?id=" + currentItem.id
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({"viewpoint.loading": true})
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})